package com.pimee.web.sys;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.pimee.common.util.shiro.ShiroUtils;
import com.pimee.model.SysNotice;
import com.pimee.service.admin.ISysNoticeService;
import com.pimee.web.AbstractController;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("admin/sys/notice")
public class SysNoticeController extends AbstractController {

	private String prefix = "admin/sys/notice";
    @Autowired
    private ISysNoticeService sysNoticeService;
    
    @ApiOperation("查询通知分页列表")
    @RequiresPermissions("system:notice:list")
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public Object pageList(@RequestParam Map<String, Object> params, ModelMap modelMap) {
		PageInfo<SysNotice> pageList = sysNoticeService.pageList(params);
		return setSuccessModelMap(modelMap, pageList);
	}
    
    @ApiOperation("进入新增通知页")
    @GetMapping("/toAdd")
    public String toAdd() {
        return prefix + "/add";
    }

    @ApiOperation("保存新增通知信息")
    @RequiresPermissions("system:notice:add")
    @PostMapping("/add")
    @ResponseBody
    public Object addSave(@Validated SysNotice param, ModelMap modelMap) {
    	String loginName = ShiroUtils.getLoginName();
    	param.setCreateBy(loginName);
    	param.setUpdateBy(loginName);
    	sysNoticeService.insertNotice(param);
        return setSuccessModelMap(modelMap);
    }

    @ApiOperation("进入修改通知页")
    @GetMapping("/toEdit/{noticeId}")
    public String edit(@PathVariable("noticeId") Long noticeId, ModelMap modelMap) {
    	modelMap.put("notice", sysNoticeService.selectByKey(noticeId));
        return prefix + "/edit";
    }

    @ApiOperation("保存修改通知信息")
    @RequiresPermissions("system:notice:edit")
    @PostMapping("/edit")
    @ResponseBody
    public Object editSave(@Validated SysNotice param, ModelMap modelMap) {
    	param.setUpdateBy(ShiroUtils.getLoginName());
        sysNoticeService.updateNotice(param);
        return setSuccessModelMap(modelMap);
    }
    
    @ApiOperation("删除通知")
    @RequiresPermissions("system:notice:remove")
    @PostMapping("/remove/{noticeId}")
    @ResponseBody
    public Object remove(@PathVariable("noticeId") Long noticeId, ModelMap modelMap) {
    	sysNoticeService.deleteByKey(noticeId);
        return this.setSuccessModelMap(modelMap);
    }
}
